﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace daq_test_cs
{
    
    public class USBServerWrapper
    {
        /// <summary>
        /// 设备类型枚举
        /// </summary>
        public enum DeviceType
        {
            DeviceType_DAQ217,  // DAQ217设备类型
            DeviceType_DAQ216,  // DAQ216设备类型
            DeviceType_DAQ207,  // DAQ207设备类型
            DeviceType_DAQ206,  // DAQ206设备类型
            DeviceType_DAQ125,  // DAQ125设备类型
            DeviceType_DAQ322,  // DAQ322设备类型
        }

        private const string DllPath = "./libdaq-4.0.0.dll";

        /// <summary>
        /// 创建新的USB服务器实例
        /// </summary>
        /// <param name="device_type">要管理的设备类型</param>
        /// <returns>返回USB服务器实例的指针，失败返回IntPtr.Zero</returns>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr USBServer_New(DeviceType device_type);

        /// <summary>
        /// 删除USB服务器实例，释放相关资源
        /// </summary>
        /// <param name="handle">USB服务器实例的指针</param>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern void USBServer_Delete(IntPtr handle);

        /// <summary>
        /// 开始搜索USB设备
        /// </summary>
        /// <param name="handle">USB服务器实例的指针</param>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern void USBServer_StartSearch(IntPtr handle);

        /// <summary>
        /// 根据设备索引获取客户端连接
        /// </summary>
        /// <param name="handle">USB服务器实例的指针</param>
        /// <param name="device_index">设备索引</param>
        /// <returns>返回设备客户端的指针，失败返回IntPtr.Zero</returns>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr USBServer_GetClientByIndex(IntPtr handle, uint device_index);

        /// <summary>
        /// 获取当前发现的设备总数
        /// </summary>
        /// <param name="handle">USB服务器实例的指针</param>
        /// <returns>返回发现的设备数量</returns>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern uint USBServer_GetDeviceCount(IntPtr handle);

        // 保留旧接口以兼容现有代码
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr USBServer_GetBusNumbers(IntPtr handle, out byte bus_count);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr USBServer_GetDeviceAddresses(IntPtr handle, byte bus_number, out byte device_count);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr USBServer_GetClient(IntPtr handle, byte bus_number, byte device_address);
    }
}

